package com.banzhi.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.CityPartners;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.AndroidUtil;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.CircleImageView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MyPartnerActivity extends BaseActivity {

    private ImageView iv_back;
    private CircleImageView ivHeader;
    private TextView tvName;
    private TextView tvNo;
    private TextView tvZhijie;
    private TextView tvJianjie;
    private TextView tvTotal;
    private TextView tvCanGet;
    private TextView tvList;
    private TextView tvMy;

    private CityPartners cityPartners;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_partner);
        Translucent.setTranslucent(this);
        setViews();
        loadData();
    }

    private void setViews() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivHeader = (CircleImageView) findViewById(R.id.ivHeader);
        tvName = (TextView) findViewById(R.id.tvName);
        tvNo = (TextView) findViewById(R.id.tvNo);
        tvZhijie = (TextView) findViewById(R.id.tvZhijie);
        tvJianjie = (TextView) findViewById(R.id.tvJianjie);
        tvTotal = (TextView) findViewById(R.id.tvTotal);
        tvCanGet = (TextView) findViewById(R.id.tvCanGet);
        tvList = (TextView) findViewById(R.id.tvList);
        tvList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyPartnerActivity.this, AwardHistoryActivity.class));
            }
        });
        tvMy = (TextView) findViewById(R.id.tvMy);
        tvMy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyPartnerActivity.this, MyRecommendActivity.class));
            }
        });
    }

    private void loadData() {
//        LoadingDialogUtils.showProgress(this);
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.citypartners);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LoadingDialogUtils.dismiss();
                LogUtils.i("Result", result);
                try {
                    JSONObject object = new JSONObject(result);
                    int code = object.getInt("code");
                    if (code == 0) {
                        cityPartners = new Gson().fromJson(object.getJSONObject("data").toString(), CityPartners.class);
                        setViewContent();
                    } else {
                        ToastUtils.makeToastShort(object.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("AddAddressImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    private void setViewContent() {
        if (!AndroidUtil.isEmpty(cityPartners.getAvatar()))
            Glide.with(this).load(cityPartners.getAvatar()).into(ivHeader);
        tvName.setText(cityPartners.getNickname());
        tvNo.setText("编号：" + cityPartners.getId());
        tvZhijie.setText(AndroidUtil.isEmpty(cityPartners.getDirect()) ? "0" : cityPartners.getDirect());
        tvJianjie.setText(AndroidUtil.isEmpty(cityPartners.getIndirect()) ? "0" : cityPartners.getIndirect());
        tvTotal.setText(AndroidUtil.isEmpty(cityPartners.getPartnermoneytotal()) ? "0" : cityPartners.getPartnermoneytotal());
        tvCanGet.setText(AndroidUtil.isEmpty(cityPartners.getPartnermoney()) ? "0" : cityPartners.getPartnermoney());
    }


}
